#-*- coding:utf-8 -*-

import tornado.web
import datetime

from bson.objectid import ObjectId

class UserBaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        return self.get_secure_cookie('username')


#博客首页，展示博客列表
class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        dbblog = self.application.db.blog
        blog_list = dbblog.find()
        context = {'blog_list':blog_list}
        self.render('blog.html',**context)


class HelloHandler(tornado.web.RequestHandler):
    def get(self):
    
        self.write("hello world")

class LoginHandler(tornado.web.RequestHandler):
    def get(self):
        context = {
                   'password_is_wrong':False,
                   'name_or_pws_is_none':False}
        self.render('login.html',**context)
   
    def post(self):
        password_is_wrong = False
        name_or_pws_is_none = False
        username = self.get_argument('username',None)        
        password = self.get_argument('password',None)
        
        if username and password:
            user = self.application.db.user.find_one({'name':username})        
            print user
            if user and user['password'] == password :
                self.set_secure_cookie('username',user['name'])        
                return self.redirect('/blog')
            else:
                password_is_wrong = True
        else:
            name_or_pws_is_none = True
        context = {
                    'password_is_wrong':password_is_wrong,
                    'name_or_pws_is_none':name_or_pws_is_none}
        self.render('login.html', **context)

class LogoutHandler(UserBaseHandler):
    def get(self):
        self.clear_cookie("username")
        self.redirect('/login')

#博客首页，展示博客列表
class BlogHandler(UserBaseHandler):
    def get(self):
        dbblog = self.application.db.blog
        blog_list = dbblog.find()
        context = {'blog_list':blog_list,
                    'current_user':self.current_user}
        self.render('blog.html',**context)

class EditHandler(UserBaseHandler):
    def get_blog(self):
        blog = dict(
                author = self.current_user,
                date =  datetime.datetime.now(),
                title = self.get_argument('title',None),
                textmd = self.get_argument('textmd',None),
                texthtml = self.get_argument('texthtml',None),
                abstract = self.get_argument('abstract',None)
            )
        return blog
        
    #@tornado.web.authenticated
    def get(self,id=None):
        if id:
            blog = self.application.db.blog.find_one({'_id':ObjectId(id)})
            context = {'blog_id':id,
                    'blog':blog}            
        else:                        
            context = {'blog_id':id,
                    'blog':None}
        self.render('edit.html',**context)
            
    #@tornado.web.authenticated
    def post(self,id=None):
        if id:
            blog = self.get_blog()
            blog += {'_id':ObjectId(id)}
            self.application.db.blog.save(blog)
            self.redirect('/detail/' + str(blog_id))            
        else:
            blog = self.get_blog()
            blog_id = self.application.db.blog.insert(blog)
            self.redirect('/detail/' + str(blog_id))

class DetailHandler(UserBaseHandler):
    def get(self,id=None):
        if id:
            blog = self.application.db.blog.find_one({'_id':ObjectId(id)})
            context = {'blog':blog,
                       'comment_list':{}                
            }
            self.render('detail.html',**context)
        else:
            self.redirect('/blog')           
